package com.shlh.saas.service.canton.exhibitor.impl;

import com.shlh.saas.entity.canton.exhibitor.BaseCantonFairExhibitor;
import com.shlh.saas.entity.canton.exhibitor.CantonFairExhibitor125;
import com.shlh.saas.mapper.canton.exhibitor.CantonFairExhibitor125Mapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 第125届广交会展商处理器
 */
@Component
@Slf4j
public class Session125ExhibitorProcessor extends AbstractCantonFairExhibitorService implements SessionExhibitorProcessor {

    @Autowired
    private CantonFairExhibitor125Mapper exhibitor125Mapper;

    @Override
    public String getSessionNumber() {
        return "125";
    }

    @Override
    public List<CantonFairExhibitor125> searchByKeyword(String keyword, int offset, int limit) {
        try {
            if (!StringUtils.hasText(keyword)) {
                return exhibitor125Mapper.selectAllWithPage(offset, limit);
            }

            // 定义搜索字段
            List<String> searchFields = Arrays.asList(
                "company_name", "contact_person", "contact_address", "postal_code",
                "website", "booth_number", "main_products", "company_type", 
                "company_scale", "establishment_year", "recent_exhibition_history"
            );

            String condition = buildKeywordSearchCondition(keyword, searchFields);
            return exhibitor125Mapper.searchByKeywordWithPage(condition, offset, limit);
            
        } catch (Exception e) {
            log.error("第125届展商关键词搜索失败", e);
            throw e;
        }
    }

    @Override
    public List<CantonFairExhibitor125> searchWithFilters(String keyword, String product, String category,
                                                        String companyType, String companyScale, String hasContact,
                                                        int offset, int limit) {
        try {
            // 构建基础搜索条件
            StringBuilder condition = new StringBuilder();

            if (StringUtils.hasText(keyword)) {
                List<String> searchFields = Arrays.asList(
                    "company_name", "contact_person", "contact_address", "postal_code",
                    "website", "booth_number"
                );
                condition.append(buildKeywordSearchCondition(keyword, searchFields));
            }

            // 添加筛选条件
            String filterCondition = buildFilterCondition(companyType, companyScale, product, hasContact);
            if (StringUtils.hasText(filterCondition)) {
                if (!condition.isEmpty()) {
                    condition.append(" AND ");
                }
                condition.append(filterCondition);
            }



            if (condition.isEmpty()) {
                return exhibitor125Mapper.selectAllWithPage(offset, limit);
            } else {
                return exhibitor125Mapper.searchWithFiltersAndPage(condition.toString(), offset, limit);
            }
            
        } catch (Exception e) {
            log.error("第125届展商筛选搜索失败", e);
            throw e;
        }
    }

    @Override
    public long countByKeyword(String keyword) {
        try {
            if (!StringUtils.hasText(keyword)) {
                return exhibitor125Mapper.selectCount(null);
            }

            List<String> searchFields = Arrays.asList(
                "company_name", "contact_person", "contact_address", "postal_code",
                "website", "booth_number", "main_products", "company_type", 
                "company_scale", "establishment_year", "recent_exhibition_history"
            );

            String condition = buildKeywordSearchCondition(keyword, searchFields);
            return exhibitor125Mapper.countByKeyword(condition);
            
        } catch (Exception e) {
            log.error("第125届展商关键词统计失败", e);
            throw e;
        }
    }

    @Override
    public long countWithFilters(String keyword, String product, String category,
                               String companyType, String companyScale, String hasContact) {
        try {
            StringBuilder condition = new StringBuilder();

            if (StringUtils.hasText(keyword)) {
                List<String> searchFields = Arrays.asList(
                    "company_name", "contact_person", "contact_address", "postal_code",
                    "website", "booth_number"
                );
                condition.append(buildKeywordSearchCondition(keyword, searchFields));
            }

            String filterCondition = buildFilterCondition(companyType, companyScale, product, hasContact);
            if (StringUtils.hasText(filterCondition)) {
                if (!condition.isEmpty()) {
                    condition.append(" AND ");
                }
                condition.append(filterCondition);
            }

            if (condition.isEmpty()) {
                return exhibitor125Mapper.selectCount(null);
            } else {
                return exhibitor125Mapper.countWithFilters(condition.toString());
            }
            
        } catch (Exception e) {
            log.error("第125届展商筛选统计失败", e);
            throw e;
        }
    }

    @Override
    public CantonFairExhibitor125 getById(Long id) {
        return exhibitor125Mapper.selectById(id);
    }

    @Override
    public BaseCantonFairExhibitor convertToBase(Object exhibitor) {
        if (!(exhibitor instanceof CantonFairExhibitor125)) {
            return null;
        }
        return convertToBaseExhibitor(exhibitor, "第125届", true);
    }

    @Override
    public BaseCantonFairExhibitor convertToBaseForExport(Object exhibitor) {
        if (!(exhibitor instanceof CantonFairExhibitor125)) {
            return null;
        }
        return convertToBaseExhibitor(exhibitor, "第125届", false);
    }

    @Override
    public String getFieldValue(Long exhibitorId, String fieldName) {
        CantonFairExhibitor125 exhibitor = exhibitor125Mapper.selectById(exhibitorId);
        if (exhibitor == null) {
            throw new IllegalArgumentException("未找到对应的参展商信息");
        }
        if ("mobilePhone".equals(fieldName)){
            fieldName = "mobile";
        }
        return getFieldValueByName(exhibitor, fieldName);
    }
}
